package org.pplb5.siresto;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class FormPesanAntar extends Activity {
	String noHPResto = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_form_pesan_antar);
		Button sendsms = (Button) findViewById(R.id.sendres);
		Button kembali = (Button)findViewById(R.id.back);			
		Intent i = getIntent();
        final String namaResto = i.getStringExtra("NAMA_RESTO");
        final String namaMenu = i.getStringExtra("NAMA_MENU");
		
        ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
		postParameters.add(new BasicNameValuePair("namaRestoran",
				namaResto.toString()));
		
		String res = null;

		try {
			res = CustomHttpClient.executeHttpPost(
					"http://annisadwinidia.hostei.com/getInfoRestoran.php", postParameters);

			String result = res.toString();  

			try{
				JSONArray jArray = new JSONArray(result);
				JSONObject json = jArray.getJSONObject(0);
				noHPResto = json.getString("noHandphone");
			}
			catch(JSONException e){
				Log.e("log_tag", "Error parsing data "+e.toString());
			}
		}
		catch (Exception e) {
			Log.e("log_tag","Error in http connection!!" + e.toString());     
		}
		
		
		kembali.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View arg0) {
				/*Intent meja = new Intent(getApplicationContext(),ActivityDetilRestoran.class);
				meja.putExtra("NAMA_RESTO", namaResto);
				startActivity(meja);*/
				finish();
				}
		});
		
		sendsms.setOnClickListener(new OnClickListener(){
	
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				EditText namaPa = (EditText) findViewById(R.id.namaPA);
				EditText alamatPa = (EditText) findViewById(R.id.alamatPA);
				EditText nohpPa = (EditText) findViewById(R.id.noHPPA);
				EditText noKtpPa = (EditText) findViewById(R.id.noKTPPA);
				EditText jumlah1 =(EditText) findViewById(R.id.jumlah1PA);
				EditText jumlah2 =(EditText) findViewById(R.id.jumlah2PA);
				EditText jumlah3 =(EditText) findViewById(R.id.jumlah3PA);
				TextView menu = (TextView) findViewById(R.id.TextView07);
				menu.setText(namaMenu);
				
				String namaPelanggan = namaPa.getText().toString();
				String nohpPelanggan = nohpPa.getText().toString();
				String alamatPelanggan = alamatPa.getText().toString();
				String noKtpPelanggan = noKtpPa.getText().toString();
				String jmlh1 = jumlah1.getText().toString();
				String jmlh2 = jumlah2.getText().toString();
				String jmlh3 = jumlah3.getText().toString();
				String namaMenu = menu.getText().toString();
				//String noHp = "5544";
				
				String isiSms = "Nama Pelanggan :" +namaPelanggan+" dengan no KTP "+noKtpPelanggan+" memesan :"+jmlh1+" "+namaMenu;
				isiSms+= ", "+jmlh2+" "+namaMenu+", "+jmlh3+" "+namaMenu+" "+" dikirim ke alamat :"+alamatPelanggan+", no HP yang dapat dihubungi "+nohpPelanggan; 
				
				SmsManager sms = SmsManager.getDefault();
				sms.sendTextMessage(noHPResto, null, isiSms, null, null);
				//Toast.makeText(FormPesanAntar.this, "Pesan Anda telah berhasil dikirim!", Toast.LENGTH_SHORT).show();
				//Intent detil = new Intent(getApplicationContext(),DetilRestoran.class);
				//daftarMenu.putExtra(", namaResto);
				//startActivity(detil);
				//finish();
				
				displayAlert();
				
			}});
	}

	public  void displayAlert()
    {
     new AlertDialog.Builder(this).setMessage("Pesan telah berhasil dikirim!")  
           .setPositiveButton("Kembali ke Menu Utama",  
                   new DialogInterface.OnClickListener() {  
               			public void onClick(DialogInterface dialog, int whichButton){
               				Intent detil = new Intent(getApplicationContext(),ActivityUtama.class);
            				startActivity(detil);
               }  
               })    
           .setNeutralButton("Ok, Thanks!",  
              new DialogInterface.OnClickListener() {  
              public void onClick(DialogInterface dialog, int whichButton){
            	  Intent intent = new Intent();
            	  intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            	  finish();
            	  System.exit(0);
              }  
              })  
           .show(); 
    }
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.form_pesan_antar, menu);
		return true;
	}

}
